package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.domain.Label;
import cc.linker.steplink.crm.merchanth5.service.acs.DynamicService;
import cc.linker.steplink.crm.service.ILabelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mongodb.util.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private ILabelService labelService;
    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private UserInfoUtil userInfoUtil;


    /*
     * 标签列表
     */
    @GetMapping("/list")
    public Object list(
            HttpServletRequest request,
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize)
            throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        PageHelper.startPage(pageIndex, pageSize);
        Map<String, Object> labelMap = new HashMap<>();
        String orgId = userinfo.getOrgId();
        labelMap.put("orgId", orgId);
        return new PageInfo<>(labelService.listLabel(labelMap));
    }

    /**
     * 单个标签详情
     *
     * @return
     */
    @GetMapping("/get")
    public Object get(String labelId) throws Exception {
        return labelService.getLabel(labelId);
    }

    /*
     * 添加标签
     */
    @PostMapping("/add")
    public void add(HttpServletRequest request, @RequestBody Map<String, Object> label) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Label record = new Label();
        String uuid = UUID.randomUUID().toString();
        record.setLabelId(uuid);
        String labelName = (String) label.get("labelName");
        record.setLabelName(labelName);
        Integer labelKind = Integer.parseInt(label.get("labelKind").toString());
        record.setLabelKind(labelKind);
        Integer labelType = Integer.parseInt(label.get("labelType").toString());
        record.setLabelType(labelType);
        String orgId = userinfo.getOrgId();
        record.setOrgId(orgId);
        record.setLabelDescription(null);
        //自动
        if (label.get("autoSetup") != null) {
            Map autoSetup = (Map) label.get("autoSetup");
            record.setAutoSetup(JSON.serialize(autoSetup));
        }
        labelService.addLabel(record);
        String adminId = userinfo.getAdminId();
        dynamicService.recording(adminId, orgId, 5, "新增"+labelName);
    }


    /*
     * 更新标签
     */
    @PostMapping("/update")
    public void update(HttpServletRequest request,
            @RequestBody Map<String, Object> label) throws Exception {
        String labelId = (String) label.get("labelId");
        String labelName = (String) label.get("labelName");
        String orgId = null;
        Label record = new Label();
        record.setLabelId(labelId);
        record.setLabelName(labelName);
        Integer labelKind = Integer.parseInt(label.get("labelKind").toString());
        record.setLabelKind(labelKind);
        Integer labelType = Integer.parseInt(label.get("labelType").toString());
        record.setLabelType(labelType);
        PcUserInfo userInfo = this.userInfoUtil.getPcUserInfo(request);
        orgId = userInfo.getOrgId();
        record.setOrgId(orgId);
        //自动
        if (label.get("autoSetup") != null) {
            Map autoSetup = (Map) label.get("autoSetup");
            record.setAutoSetup(JSON.serialize(autoSetup));
        }
        labelService.updateLabel(record);
    }


    /*
     * 删除标签
     */
    @PostMapping("/del")
    public void del(@RequestBody Map<String, Object> label, HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String labelId = (String) label.get("labelId");
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        Map<String, Object> label1 = labelService.getLabel(labelId);
        dynamicService.recording(adminId, orgId, 6, "删除"+label1.get("labelName"));
        labelService.delLabel(labelId);
    }

    /**
     * 获取所有手动标签
     */
    @GetMapping("/alllabels")
    public Object alllabels(HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return labelService.getLabelsByOrgId(orgId);
    }

    /**
     * 获取所有文章标签
     */
    @GetMapping("/getarticlelabels")
    public Object getArticleLabels(HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return labelService.getArticleLabelsByOrgId(orgId);
    }

    /**
     * 添加用户标签
     */
    @PostMapping("/addcustomlabel")
    public void addCustomLabel(
            @RequestBody Map<String, Object> labelList
    ) throws Exception {
        List<Map<String,Object>> list = (List) labelList.get("labelList");
        String customerId = (String) labelList.get("customerId");
        labelService.addCustomLabel(list, customerId);
    }

    /**
     * 标签数据同步接口
     *
     * @param param
     * @return
     * @throws Exception
     */
    @GetMapping("/getlabellist")
    public Object getLabelList(String orgId, Integer labelType) throws Exception {
        return labelService.getLabelList(orgId, labelType);
    }
}
